/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services.Impl;

import java.util.Collection;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import models.UserRole;
import models.Users;
import services.interfaces.IUserService;

/**
 *
 * @author amega
 */
@Stateless
@LocalBean
public class UserServiceImpl extends GenericServiceImpl<Users> implements IUserService {

    
    @PersistenceContext(unitName="orsPU")
    private EntityManager em;
    
    
    public UserServiceImpl() {
        super(Users.class);
    }
    

    @Override
    protected EntityManager getEntityManager() {
        return this.em;
    }

    @Override
    public Collection<Users> getUsersByRole(UserRole role) {
        Query q = em.createNamedQuery("Users.findByRole").setParameter("role", role);
        Collection entities = q.getResultList();
        return  entities;
    }
    
    @Override
    public Users getUserByName(String name) {
        Query q = em.createNamedQuery("Users.findByName").setParameter("name", name);
        Users u = (Users)q.getSingleResult();
        return u;
    }
    
    @Override
    public Collection<Users> getUserByPhone(String phone) {
        Query q = em.createNamedQuery("Users.findByPhone").setParameter("phone", phone);
        Collection entities = q.getResultList();
        return  entities;
    }

    @Override
    public Users getUsersByEmail(String email) {
        Query q = em.createNamedQuery("Users.findByEmail").setParameter("email", email);
        Users u = (Users)q.getSingleResult();
        return u;
    }

    @Override
    public Users getUsersByNameAndPass(String login, String password) {
        try {
            Query q = em.createNamedQuery("Users.findByNameAndPass").setParameter("name", login).setParameter("password", password);
            Users u = (Users)q.getSingleResult();
             return u;
        } catch (Exception e) {
            return  null;
        }
        
       
    }
 
    
}
